package com.viphuli.app.tool.fragment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ListView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.DateUtils;
import com.offroader.utils.JsonUtils;
import com.offroader.utils.LogUtils;
import com.offroader.utils.StringUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.adapter.ArrangeActionTypeAdapter;
import com.viphuli.app.tool.bean.part.AccountUser;
import com.viphuli.app.tool.bean.part.ArrangeType;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.handler.ArrangeWeekViewResponseHandler.DayUserArrangeHolder;
import com.viphuli.app.tool.http.ApiRequest;
import com.viphuli.app.tool.utils.OtherUtils;

/**
 * 选择排班类型
 * 
 * @author li.li
 *
 */
public class ArrangeActionTypeFragment extends BaseProgressBottomDialogFragment {

	@Bind(R.id.id_listview)
	protected ListView listview;

	private ArrangeActionTypeAdapter adapter;

	private List<ArrangeType> list = new ArrayList<ArrangeType>();

	private DayUserArrangeHolder dayUserArrangeHolder;
	
	private List<Integer> checkList;
	
	private int reqIndex;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		//无标题
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_dialog_arrange;
	}

	@Override
	protected void initParams() {

		Bundle args = getArguments();
		String data = args.getString("data");

		if (StringUtils.isNotBlank(data)) {

			dayUserArrangeHolder = JsonUtils.fromJson(data, DayUserArrangeHolder.class);

		}

	}

	@Override
	protected void initView() {

	}

	@Override
	protected void initData() {
		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {

			RequestParams params = new RequestParams();
			params.put("circle_id", tokenKeeper.getUser().circleId());

			ApiRequest.arrangeActionType.request(this, params);

		}
	}

	@OnClick(R.id.id_arrange_btn)
	public void clickBtn() {

		checkList = adapter.getCheck();

		if (dayUserArrangeHolder != null) {

			AccountUser toUser = dayUserArrangeHolder.getUser();
			Date date = dayUserArrangeHolder.getDate();
			
			LogUtils.info("@@@@@@@@@@@@@排班时间@"+date.getTime()+"|"+DateUtils.format(date));
			AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

			if (tokenKeeper.isLogin()) {
				for (int check : checkList) {
					
					ArrangeType arrangeType = list.get(check);
					RequestParams params = new RequestParams();
					params.put("uid", tokenKeeper.getOpenId());
					params.put("arrange_type_id", arrangeType.getId());
					params.put("to_uid", toUser.getUid());
					params.put("time", OtherUtils.maxLength(date.getTime()));
					params.put("circle_id", tokenKeeper.getUser().circleId());
					params.put("type", 1);//1排班,2换班,3替班
					
					ApiRequest.arrangeAction.request(this, params);
					
				}

			}

		} else {//什么也没选择

			getDialog().dismiss();
		}

	}

	public ArrangeActionTypeAdapter getAdapter() {
		return adapter;
	}

	public void setAdapter(ArrangeActionTypeAdapter adapter) {
		this.adapter = adapter;
	}

	public ListView getListview() {
		return listview;
	}

	public List<ArrangeType> getList() {
		return list;
	}

	public List<Integer> getCheckList() {
		return checkList;
	}

	public int getReqIndex() {
		return reqIndex;
	}

	public void setReqIndex(int reqIndex) {
		this.reqIndex = reqIndex;
	}
	
}
